package com.thauanLopes.bacterium;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import Bacterias.HelmetBacteria;
import android.content.Context;
import android.graphics.Canvas;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.game.ThauanFramework.BitmapStorage;
import com.game.ThauanFramework.GameObject;
import com.game.ThauanFramework.Util;
import com.game.ThauanFramework.TimeManager;
import com.thauanLopes.bacterium.GameManages.CashManager;
import com.thauanLopes.bacterium.GameManages.GameManager;
import com.thauanLopes.bacterium.GameManages.GameManagerFactory;
import com.thauanLopes.bacterium.GameManages.ScoreManager;
import com.thauanLopes.smagasmaga.R;

public class GameSceneView extends View implements Runnable {

	private String TAG = "GameScene";

	private boolean running = true;
	private boolean lost = false;

	public GameManager currentGameManager;
	private GameManagerFactory gameManagerFactory;

	// Thread Interval
	private int interval = 10;
	Thread mainThread;

	// To catch all the scene game objects and null them.
	public static List<GameObject> cemetery = new ArrayList<GameObject>();

	public static int canvasHeight;
	public static int canvasWidth;

	public GameSceneView(Context context) {
		super(context);
		running = true;

		this.setFocusable(true);

		ScoreManager.getInstance().Reset();

		this.setFocusable(true);
		mainThread = new Thread(this);

	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		canvasWidth = w;
		canvasHeight = h;

		Util.setCanvasWidth(w);
		Util.setCanvasHeight(h);

		ChangeGameMode(GameManagerFactory.MODE_NORMAL);

		mainThread.start();

		// this.setBackgroundResource(R.drawable.bacterium_fundo_medium);
	}

	public boolean onTouchEvent(MotionEvent event) {

		boolean returnBool = false;

		if (currentGameManager != null)
			returnBool = currentGameManager.onTouch(event);

		Log.i(TAG, String.valueOf(event.getPointerCount()));

		return returnBool;

	}

	// ------------------------------------------------------------- START
	public void Start() {
	}

	// ------------------------------------------------------------- UPDATE
	public void Update() {

		if (this.lost) {
			Util.getCurrentActivity().finish();
			this.SetAlive(false);
			if (ScoreManager.getInstance().getScore() > ScoreManager
					.getHighScore()) {
				ScoreManager
						.setHighScore(ScoreManager.getInstance().getScore());
				ScoreManager.getInstance().Reset();
			}
			return;
		}

		// GameManager Update
		if (currentGameManager != null)
			this.currentGameManager.Update(TimeManager.getInstance()
					.getDeltaTime());

		// ScoreManager Update
		ScoreManager.getInstance().Update(
				TimeManager.getInstance().getDeltaTime());
	}

	// Draw
	@Override
	public void draw(Canvas canvas) {
		super.draw(canvas);

		// BG
		canvas.drawBitmap(
				BitmapStorage.getInstance().getBitmap(
						R.drawable.monstro_background), 0, 0, Util.getPaint());

		// ScoreManager Draw
		ScoreManager.getInstance().Draw(canvas);

		// CashManager Draw
		//CashManager.getInstance().Draw(canvas);

		try {

			// ============================================================
			// Destroy elements no more used
			if (!cemetery.isEmpty()) {

				for (GameObject gb : cemetery) {
					gb.Destroy();
					gb = null;
				}
			}
			cemetery.clear();
			// ===========================================================

			// GameManager Draw
			if (this.currentGameManager != null)
				this.currentGameManager.Draw(canvas);

		} catch (Exception e) {

		}
	}

	public void SetAlive(boolean bool) {
		this.running = bool;
	}

	public boolean isAlive() {
		return this.running;
	}

	@Override
	public void run() {
		this.Start();

		while (running) {
			try {
				Thread.sleep(interval);
			} catch (InterruptedException e) {
				running = false;
				Log.i(TAG, "Erro no processo de Thread", e);
			}

			Update();

			// Reload drawables on the screen
			postInvalidate();
		}

	}

	// ----------------------------- Getters And Setters

	public boolean isLost() {
		return lost;
	}

	public void setLost(boolean lost) {
		this.lost = lost;
	}

	public void ChangeGameMode(int gameManagerIndex) {
		if (this.gameManagerFactory == null) {
			this.gameManagerFactory = new GameManagerFactory(cemetery, this);
			gameManagerFactory.SetInstance(this.gameManagerFactory);
		}

		this.currentGameManager = this.gameManagerFactory
				.Create(gameManagerIndex);
	}

	public ArrayList<GameObject> gos() {
		return (ArrayList<GameObject>) this.currentGameManager.getGos();
	}
}
